<?php
    include_once "config/config.php"; include_once "functions/getnames.php"; include_once "functions/genericfunctions.php";

function SpausdintiRezultata()
{
        $var = $_SESSION['upddata'];       // uzduoties var
        $msg =  $_SESSION['stogas'];        // klaidos zinute
        $success = $_SESSION['laimas'];     // sutampa su teisinga lentele
        $posted = $_SESSION['metras'];
		global $tempDB; global $workDB;
//	    $esamasirasas = $_SESSION['update_senas'];
//    	$naujasIrasas = $_SESSION['update_naujas'];

//        $c_task = mysql_query("SELECT komanda, salyga FROM uzduotis_update WHERE id =". $var."", $workDB);
//        $c_komanda = mysql_fetch_assoc($c_task);
		$c_komanda = $_SESSION['upddata'];
        echo('<p>'.$c_komanda['salyga'].'</p><br />');

        echo('<p><strong>Jūsų rezultatas:</p></strong>');
        if (!$success)
            echo('<p style="color: red;">'.$msg.'</p><br />');
        else
            echo('<p style="color: green;">'.$msg.'</p><br />');
        
        echo "<br /><p><strong>Pradinė lentelė:</strong></p>";

        $name = getTblUpdate($c_komanda['komanda']);
        $fields = getCols($name);
 		$table1 = Nuskaityti($name, $fields, $tempDB);
		spausdinti($name, $table1, $table1, $fields);
 		if ($_SESSION['laimas']){
 			mysql_query("START TRANSACTION", $tempDB);
			mysql_query($posted, $tempDB);
 			echo "<p><strong>Jūsų atnaujinti duomenys:</strong></p><br />";

	        $p_name = getTblUpdate($posted);

        	$p_fields = getCols($p_name);
			
			$table2 = Nuskaityti($p_name, $p_fields, $tempDB);

			spausdinti($p_name, $table2, $table1, $p_fields);
			mysql_query("ROLLBACK", $tempDB);
	
 		}
                echo('<p>Jūsų įvesta komanda:</p><br />');
                $str = str_replace('<', '< ', $posted);
                if($success)
                    echo('<pre class="code"><br/>');
                else
                    echo('<pre class="codeERR"><br/>');
                echo('<code>');
                echo($str);
                echo('<br /> </code></pre>');        


		    echo '<tr>';
		    
	return true;
}

function Nuskaityti($name, $fields, $db) 
{
		$result = mysql_query("SELECT * FROM {$name}", $db);
		$tbl = fillArrays($result, $fields);		    

	return $tbl;	
}

function spausdinti($name, $tbl1, $tbl2, $fields)
{
		echo '<table border="1" class="text" cellpadding="0" cellspacing="0" width="350px">';
	    	echo '<tr><td colspan='.count($fields).' class="head"><span>'.$name.'</span></td></tr><tr>';
			    for($i = 0; $i < count($fields); $i++) {
		            echo '<td><span  class="head">'.$fields[$i].'</span></td>';
			    }
	        echo '</tr>';
			for ($i=0; $i < count($tbl1); $i++) {
//				var_dump(lyginti($name1, $name2, count($fields)));

				if (!lyginti($tbl1[$i], $tbl2[$i], $fields, count($fields))) {
					echo '<tr class="highlighted">';	
				}
				else {
					echo "<tr>";
				}
				for ($j=0; $j < count($fields); $j++) { 
					echo '<td class="textp"><span>'.$tbl1[$i][$fields[$j]].'</span></td>';
				}
					
				echo "<tr>";
			}		        
       
 			 echo "</table> <br />";
	return TRUE;	
}

function lyginti($table1, $table2, $fields, $k)
{
	for ($i=0; $i < $k; $i++) {
		if ($table1[$fields[$i]] != $table2[$fields[$i]]) {
			return FALSE;	
		} 
		
	}
	return TRUE;
}		
?>